<!-- 护理项目列表页 -->
<template>
  <div class="min-h serveProject bg-wt">
    <!-- //搜索框子组件 -->
    <SearchForm
      :searchData="pagination"
      @handleClear="handleClear"
      @handleReset="handleReset"
      @handleSearch="getList"
    ></SearchForm>
    <!-- //新增按钮子组件 -->
    <DialogForm
      ref="formRef"
      :visible="visible"
      :data="formDataRef"
      :title="title"
      @handleClose="handleClose"
      @handleAdd="handleAdd"
      @handleEdit="handleEditForm"
    ></DialogForm>

    <!-- //列表子组件 -->
    <TableList
      :data="data"
      :bordered="bordered"
      :hover="hover"
      :size="size"
      :total="total"
      :pagination="pagination"
      @onPageChange="onPageChange"
      @handleBulid="handleBulid"
      @handleEdit="handleEdit"
    ></TableList>
  </div>
</template>

<script setup lang="jsx">
import { onMounted, ref } from 'vue'
import {
  getProjectDetails,
  getProjectList,
  projectAdd,
  projectUpdate
} from '@/api/serve'
import TableList from './components/TableList.vue'
import SearchForm from './components/SearchForm.vue'
import DialogForm from './components/DialogForm.vue'
import { MessagePlugin } from 'tdesign-vue-next'

const data = ref([])
const total = ref(0)
const formRef = ref({})
const bordered = ref(true)
const hover = ref(false)
const size = ref('medium')

const pagination = ref({
  pageNum: 1,
  pageSize: 5
})

const onPageChange = (pageInfo) => {
  console.log(pageInfo)

  pagination.value.pageNum = pageInfo.current
  pagination.value.pageSize = pageInfo.pageSize

  getList()
}

const getList = async () => {
  const result = await getProjectList(pagination.value)

  if (result.code == 200) {
    data.value = result.data.records
    total.value = result.data.total
  }
}

onMounted(() => {
  getList()
})

const handleClear = (val) => {
  if (val == 'name') {
    pagination.value.name = ''
  } else if (val == 'status') {
    delete pagination.value.status
  }
  getList()
}

const handleReset = () => {
  pagination.value = {
    pageNum: 1,
    pageSize: 5
  }
  getList()
}

const visible = ref(false)
const title = ref('')
// 新增按钮点击事件
const handleBulid = () => {
  title.value = '新增'
  visible.value = true
}
//关闭修改或新增对话框
const handleClose = () => {
  visible.value = false
}
//发送请求添加数据
const handleAdd = async (formData) => {
  const result = await projectAdd(formData)
  if (result.code == 200) {
    MessagePlugin.success('操作成功')
    visible.value = false
    formRef.value.handleClear()
    getList()
  } else {
    MessagePlugin.error(result.msg)
  }
}

//编辑事件
const formDataRef = ref([])
const handleEdit = async (id) => {
  const result = await getProjectDetails(id)
  //成功
  if (result.code == 200) {
    formDataRef.value = result.data
    title.value = '编辑'
    visible.value = true
  } else {
    MessagePlugin.error(result.msg)
  }
}
//更新事件
const handleEditForm = async (formData) => {
  const result = await projectUpdate(formData)
  if (result.code == 200) {
    MessagePlugin.success('操作成功')
    visible.value = false
    formRef.value.handleClear()
    getList()
  } else {
    MessagePlugin.error(result.msg)
  }
}
</script>
